package com.shiwaixiangcun.monitor.domain;


import com.rabbit.spring.data.domain.BaseEntity;
import com.rabbit.spring.data.domain.ext.LogicDeleteable;
import com.rabbit.spring.data.domain.ext.Status;
import com.rabbit.spring.data.domain.ext.Treeable;
import com.shiwaixiangcun.monitor.enums.CategoryType;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Formula;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 工单类别
 */
@Entity
@Table(name = "ms_work_order_category")
@DynamicUpdate
@DynamicInsert
public class WorkOrderCategory extends BaseEntity<Long> implements Treeable<Long, WorkOrderCategory>, LogicDeleteable {


    private String name;

    @Enumerated(EnumType.STRING)
    @Column(name = "type_")
    private CategoryType type;

    private Long parentId;
    private String parentIds;  //父路径，以 / 分隔
    private Integer weight;
    private String icon;
    private boolean expand = false;
    private boolean defaulted;

    @Enumerated(EnumType.STRING)
    private Status status = Status.ENABLE;

    @Formula(value = "(select count(*) from ms_work_order_category f_t where f_t.parent_id = id)")
    private boolean hasChildren;

    @Transient
    private List<WorkOrderCategory> children = new ArrayList<>();

    private Boolean deleted;

    public WorkOrderCategory() {
    }

    public CategoryType getType() {
        return type;
    }

    public void setType(CategoryType type) {
        this.type = type;
    }

    public boolean isDefaulted() {
        return defaulted;
    }

    public void setDefaulted(boolean defaulted) {
        this.defaulted = defaulted;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public String getParentIds() {
        return parentIds;
    }

    public void setParentIds(String parentIds) {
        this.parentIds = parentIds;
    }

    @Override
    public String makeSelfAsNewParentIds() {
        return getParentIds() + getId() + getSeparator();
    }

    @Override
    public String getSeparator() {
        return DEFAULT_SEPARATOR;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public String getIcon() {
        if (!StringUtils.isEmpty(icon)) {
            return icon;
        }
        if (isRoot()) {
            return getRootDefaultIcon();
        }
        if (isLeaf()) {
            return getLeafDefaultIcon();
        }
        return getBranchDefaultIcon();
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }


    @Override
    public boolean isRoot() {
        return getParentId() != null && getParentId() == 0;
    }

    @Override
    public boolean isLeaf() {
        return !isRoot() && !isHasChildren();

    }

    @Override
    public boolean isHasChildren() {
        return hasChildren;
    }

    public void setHasChildren(boolean hasChildren) {
        this.hasChildren = hasChildren;
    }

    /**
     * 根节点默认图标 如果没有默认 空即可
     */
    @Override
    public String getRootDefaultIcon() {
        return Treeable.DEFAULT_ROOT_ICON_CLS;
    }

    /**
     * 树枝节点默认图标 如果没有默认 空即可
     */
    @Override
    public String getBranchDefaultIcon() {
        return Treeable.DEFAULT_BRANCH_ICON_CLS;
    }

    /**
     * 树叶节点默认图标 如果没有默认 空即可
     */
    @Override
    public String getLeafDefaultIcon() {
        return Treeable.DEFAULT_LEAF_ICON_CLS;
    }


    @Override
    public boolean isExpand() {
        return this.expand;
    }

    @Override
    public void setExpand(boolean expand) {
        this.expand = expand;
    }

    @Override
    public Status getStatus() {
        return status;
    }

    @Override
    public void setStatus(Status status) {
        this.status = status;
    }

    @Override
    public List<WorkOrderCategory> getChildren() {
        return this.children;
    }

    @Override
    public void addChild(WorkOrderCategory child) {
        this.children.add(child);
    }

    @Override
    public Boolean getDeleted() {
        return deleted;
    }

    @Override
    public void setDeleted(Boolean deleted) {
        this.deleted = deleted;
    }

    @Override
    public void markDeleted() {
        this.deleted = Boolean.TRUE;
    }
}
